﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserManager_AddAdministrator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RECORDID.Text = "";
            String rID = Page.Request["ID"];
            if (rID != null && rID != "")
            {
                EntityOprHelper helper = new EntityOprHelper();
                AdminClass add = helper.GetEntityByWhere<AdminClass>(" where ID=" + rID + "");
                if (add != null)
                {
                    userName.Text = add.name;
                    userPassword.Text = add.password;
                    userLocked.SelectedIndex =add.locks;
                    userName.Enabled = false;
                    RECORDID.Text = add.ID.ToString();
                }
            }

        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {

        String userNames=userName.Text;
        String userPass=userPassword.Text;
        if (userNames == "" || userPass == "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('请输入用户名和密码')", true);
            return;
        }
        EntityOprHelper helper = new EntityOprHelper();
        String Md5_Pass = CheckClass.MD5(userPass, 32);

        if (RECORDID.Text != "")
        {
            AdminClass address = new AdminClass();
            address.name = userNames;
            address.password = Md5_Pass;
            address.locks = userLocked.SelectedIndex;
            address.ID = RECORDID.Text;

            bool b1 = helper.EditEntity<AdminClass>(address);
            if (b1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功')", true);
                userName.Text = "";
                userName.Enabled = true;
                RECORDID.Text = "";

            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败')", true);
            }

            return;
        }



       
        AdminClass oldUser=helper.GetEntityByWhere<AdminClass>(" where name='" + userNames + "'");
        if (oldUser!=null) 
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('用户已经存在!')", true);
            return;
        }

        AdminClass _address = new AdminClass();
        _address.name = userNames;
        _address.password = Md5_Pass;
        _address.locks = userLocked.SelectedIndex;

        
        
        bool b = helper.AddEntity<AdminClass>(_address);
        if (b)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功')", true);
            userName.Text = "";
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加失败')", true);
        }

    }
}
